jQuery(document).ready(function($) {
   $("#employerregform").jobUtil("setCaptcha", {
      captchaurl: showcaptchaurl
   });

   // show a simple loading indicator
   $.metadata.setType("attr", "validate"); 
   var form_validator = {
      rules: {
         email: {
           remote: {url: validateemailurl, type: "POST"}
         }, 
         name: {
           remote: {url: validatenameurl, type: "POST"}
         },   
         password2: {
            required: true, 
            equalToInForm: "#password"
         }
      },
      messages: {
         email: {
            email: "请输入正确格式的电子邮件"
         }, 
         password2: {
            equalToInForm: "两次填写的密码不一致"
         },
         vcode: {
            required: "请输入邮箱验证码"
         }
      },
      failure: function(label) {
        label.removeClass("checked"); 
         if(label.attr("for") == "email") {
            $("#employerregform #reg_vcode").hide();
         }
      },
      success: function(label) {
         if(label.attr("for") == "vcode") {
            return; 
         }
         // set &nbsp; as text for IE
         label.addClass("checked");
         if(label.attr("for") == "email") {
            $("#employerregform #reg_vcode").show();
            var email = $.trim($("#employerregform #email").val()); 
            if(!$("#employerregform #reg_getvcode").data("email"))
               $("#employerregform #reg_getvcode").data("email", email).trigger("click");
         }
      },
      submitHandler: function(form) {
         $(form).ajaxSubmit({
            dataType: "json", 
            beforeSubmit: function(data, form, options) { 
               if(!showLoading(form))
                  return false; 
               $("#regmsg", form).hide();  
            },
            success: function(data) {
              try {
                  var retmsg = data; 
                  if(retmsg.error) { 
                     $("#regmsg", form).jobUtil("showError", retmsg.error); 
                     if(/captcha_code/.test(retmsg.error)) {
                           $(".regcaptcha", form).show();
                           $("#captcha_reload").trigger("click");
                        }

                  }
                  else if(retmsg.success) { 
                     if($("#regformtab #email").length > 0) { 
                       /* remember user's email provider */
                       var email = $("#regformtab #email").val(); 
                       var emaildata = email.split("@"); 
                       if(emaildata.length > 1) {
                          storageSet("useremail_login", email);
                          storageSet("useremail_provider", emaildata[1]);
                       }
                     }

                     $("#regmsg", form).jobUtil("showSuccess", retmsg.success).
                                  fadeIn('fast').delay(1000, function() { 
                                	   window.location = jobseekerhomeurl; 
                                 });
                     
                 }
                     
              } catch(e) {
                 return false;
              }
            }
        });
      }
   };

   $("#employerregform").validate(form_validator);

   $("#employerregform #password").blur(function() { 
      if($(this).val().length <= 0)
         return;

      if(!$(this).jobUtil("strengthCheck"))
         $(this).nextAll(".label-warning").fadeIn();
      else 
         $(this).nextAll(".label-warning").fadeOut();
   })

   $("#employerregform #email").jobUtil("emailProvider");

   $("#employerregform").jobUtil("showTooltip");

   $("#employerregform #reg_getvcode").click(function(e) {
      $(this).jobUtil("getVerifyCode", {
         url: getvcodeurl,
         email: $.trim($("#employerregform #email").val())
      });
   })

   // focus first text-field 
   setFocus("employerregform");

});
